package com.anis.project.medcare.search.dialog;

import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PartInitException;

import com.anis.calendar.ui.widgets.model.Appointment;
import com.anis.calendar.ui.widgets.model.CalendarModel;
import com.anis.calendar.ui.widgets.model.Day;
import com.anis.calendar.ui.widgets.util.ui.timebars.swt.TimeBarViewer;
import com.anis.jpa.medical.model.Patient;
import com.anis.project.medcare.Activator;
import com.anis.project.medcare.editors.PatientEditor;
import com.anis.project.medcare.editors.input.PatientEditorInput;
import com.anis.project.medcare.search.table.providers.AllPatientContentProv;
import com.anis.project.medcare.search.table.providers.AllPatientTableLabelProv;
import com.anis.project.medcare.search.table.providers.SearchPatientContentProv;
import com.anis.project.medcare.search.table.providers.SearchPatientLabelProv;

import de.jaret.util.date.JaretDate;

import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.ModifyEvent;

public class AppointementDialog extends Dialog {
	private TableViewer table;
	protected boolean retrieveAll;
	private Patient selectedPatient;
	private Day day;
	private int hour;
	private int minutes;
	private int duration;
	private TimeBarViewer timeBarViewer;

	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 */
	public AppointementDialog(Shell parentShell, Day date2,TimeBarViewer timebarViewer) {
		super(parentShell);
		this.day = date2;
		this.timeBarViewer = timebarViewer;
	}

	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(5, false));

		table = new TableViewer(container, SWT.BORDER | SWT.FULL_SELECTION);
		final Table table_1 = table.getTable();
		table_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem selectedItem = table_1.getSelection()[0];
				if (selectedItem.getData() instanceof Patient) {
					selectedPatient = (Patient) selectedItem.getData();
					getOKButton().setEnabled(true);

				}
			}
		});
		table_1.setLinesVisible(true);
		table_1.setHeaderVisible(true);
		table_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 4,
				1));

		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(table,
				SWT.NONE);
		TableColumn tblclmnPatientName = tableViewerColumn_1.getColumn();
		tblclmnPatientName.setWidth(109);
		tblclmnPatientName.setText("Patient Name");

		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(table,
				SWT.NONE);
		TableColumn tblclmnPatientLastName = tableViewerColumn_2.getColumn();
		tblclmnPatientLastName.setWidth(122);
		tblclmnPatientLastName.setText("Patient Last Name");

		Group group = new Group(container, SWT.NONE);
		group.setLayout(new GridLayout(2, false));
		group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));

		Label rdvDayLabel = new Label(group, SWT.NONE);
		rdvDayLabel.setAlignment(SWT.CENTER);
		rdvDayLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,
				2, 1));

		rdvDayLabel.setText(day.getDayDate().getDay() + "-"
				+ day.getDayDate().getMonth() + "-"
				+ day.getDayDate().getYear());

		Label lblHours = new Label(group, SWT.NONE);
		lblHours.setAlignment(SWT.CENTER);
		lblHours.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		lblHours.setText("Hour");

		Label lblMinute = new Label(group, SWT.NONE);
		lblMinute.setAlignment(SWT.CENTER);
		lblMinute.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		lblMinute.setText("Minute");

		final Spinner hourSpinner = new Spinner(group, SWT.BORDER);
		hourSpinner.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				hour = Integer.valueOf(hourSpinner.getText());
			}
		});
		hourSpinner.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		hourSpinner.setMinimum(0);
		hourSpinner.setMaximum(24);

		final Spinner minuteSpinner = new Spinner(group, SWT.BORDER);
		minuteSpinner.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));

		minuteSpinner.setMinimum(0);
		minuteSpinner.setMaximum(59);

		minuteSpinner.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				minutes = Integer.valueOf(minuteSpinner.getText());
			}
		});

		Label lblDuration = new Label(group, SWT.NONE);
		lblDuration.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false,
				false, 1, 1));
		lblDuration.setText("Duration :");
		new Label(group, SWT.NONE);

		final Spinner durationSpinner = new Spinner(group, SWT.BORDER);
		durationSpinner.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER,
				false, false, 1, 1));
		new Label(group, SWT.NONE);

		durationSpinner.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				duration = Integer.valueOf(durationSpinner.getText());
			}
		});

		List<Patient> patientList = Activator.getDefault().databaseBinder()
				.retrieveAllPatient();
		table.setLabelProvider(new AllPatientTableLabelProv());
		table.setContentProvider(new AllPatientContentProv());
		table.setInput(patientList);

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		getOKButton().setEnabled(false);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(546, 360);
	}

	@Override
	protected void okPressed() {
		super.okPressed();
		JaretDate appoinementDate = new JaretDate();
		Appointment a = new Appointment(appoinementDate, hour, minutes,
				duration, selectedPatient.getPatientname());
		day.addInterval(a);
		Activator.getDefault().setModel(timeBarViewer.getModel());
	}
}
